#!/usr/bin/python
import datetime

style='''
<style>
/* Style Definitions */ 
table.MsoNormalTable {
    border-spacing:0px;
    font-size:10.0pt;
    font-family:"Times New Roman","serif";
}

.commonFont {
    font-size:11.0pt;
    font-family:"Times New Roman","serif";
}

.headTd {
border:solid windowtext 1.0pt;
       border-left:none;
background:#C5D9F1;
padding:0cm 5.4pt 0cm 5.4pt;
        font-family:"Arial","sans-serif";
        font-size:10.0pt;
}

.contentTd {
    border-top:none;
    border-left:none;
    border-bottom:solid windowtext 1.0pt;
    border-right:solid windowtext 1.0pt;
background:#FFFFFF;
padding:0cm 5.4pt 0cm 5.4pt;
        font-family:"Arial","sans-serif";
        font-size:10.0pt;
}

</style>
'''

T_s = '<table class="MsoNormalTable">'
T_e = '</table>'
R_s = '<tr>'
R_e = '</tr>'
D_h_s = '<td class="headTd">'
D_c_s = '<td class="contentTd">'
D_e = '</td>'

N=14

def get_daily_stat(date):
    mt_dict = {}
    all = 0
    stat_file_pre = "./data/mt_stat"
    date_str = date.strftime("%Y%m%d")
    stat_file = stat_file_pre + "." + date_str
    #print stat_file
    reader = open(stat_file)
    line = reader.readline()
    while line: 
        flds = line.strip().split("\t")
        mt_flds = flds[0].split("=")
        mt = mt_flds[0].split("_")[-1]
        rt_flds = flds[1].split("=")
        if mt == "ALL":
            all = mt_flds[1]
        else:
            mt_dict[mt] = (mt_flds[1], rt_flds[1])
        line = reader.readline()
    reader.close()
    # sort mt_dict by mt
    sorted_dict = sorted(mt_dict.items(), key=lambda d: int(d[0]))
    # make table row
    row_head = R_s + "\n" + D_h_s + "ColTitle"+ D_e + "\n"
    row_count = R_s + "\n" + D_c_s + date_str + D_e + "\n"
    row_ratio = R_s + "\n" + D_c_s + date_str + D_e + "\n"
    
    for item in sorted_dict:
        mt = item[0]
        count = item[1][0]
        ratio = '%.3f%%' % (float(item[1][1]) * 100)
        row_head += D_h_s + mt + D_e + "\n"
        row_count += D_c_s + count + D_e + "\n"
        row_ratio += D_c_s + ratio + D_e + "\n"

    row_head += D_h_s + "ALL" + D_e + "\n" + R_e
    row_count += D_c_s + str(all) + D_e + "\n" + R_e
    row_ratio += D_c_s + "100%" + D_e + "\n" + R_e
    return [row_head, row_count, row_ratio]

count_table = T_s + "\n"
ratio_table = T_s + "\n"
cur_date = datetime.datetime.now()
#cur_date = datetime.datetime.now() - datetime.timedelta(days=1)

[row_head, row_count, row_ratio] = get_daily_stat(cur_date)
count_table += row_head + "\n"
count_table += row_count + "\n"
ratio_table += row_head + "\n"
ratio_table += row_ratio + "\n"
for d in range(1,N):
    date = cur_date - datetime.timedelta(days=d)
    [row_head, row_count, row_ratio] = get_daily_stat(date)
    count_table += row_count + "\n"
    ratio_table += row_ratio + "\n"
count_table += T_e
ratio_table += T_e

print style
print '<p class="commonFont"><b>Mail Body Head</b></p>'
print '<span class="commonFont"><b>Table 1</b></span><br/>'
print count_table
print '<br/><br/>'
print '<span class="commonFont"><b>Table 2</b></span><br/>'
print ratio_table

